package br.edu.ufcg.lp2;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class UserTest {
	
	User u1;
	User u2;
	User u3;

	@Before
	public void setUp() throws Exception {
		// Creating three simple users
		u1 = new User("Gimli");
		u2 = new User("Legolas");
		u3 = new User("Bilbo");
	}

	@Test
	public void testUser() {
		try {
			// Testing the creation of an ordinary user
			assertEquals("Gimli", u1.getName());
			u1 = new User("Faramir");
			assertEquals("Faramir", u1.getName());
			
			// Testing the creation of a user with an empty String for a name
			u2 = new User("");
			fail("Did not break with an empty String for the name");
		} catch (Exception e) {
			assertEquals("Name cannot be empty", e.getMessage());
		}
	}

	@Test
	public void testSetName() {
		try {
			// Testing normal cases of setName
			assertEquals("Gimli", u1.getName());
			u1.setName("Faramir");
			assertEquals("Faramir", u1.getName());
			assertEquals("Legolas", u2.getName());
			u2.setName("Bono Vox");
			assertEquals("Bono Vox", u2.getName());
			assertEquals("Bilbo", u3.getName());
			u3.setName("Tom Bombadil");
			
			// Testing a set name with an empty String
			u3.setName("");
			fail("Did not break with an empty string for a name");
		} catch (Exception e) {
			assertEquals("Name cannot be empty", e.getMessage());
		}
		
		try {
			// Setting u1's name for a String made entirely of empty spaces
			u1.setName("       ");
			fail("Did not break with a name made of empty spaces");
		} catch (Exception e) {
			assertEquals("Name cannot be empty", e.getMessage());
		}
	}

	@Test
	public void testGetName() throws Exception {
		// Getting the names of u1, u2 and u3 before a set
		assertEquals("Gimli", u1.getName());
		assertEquals("Legolas", u2.getName());
		assertEquals("Bilbo", u3.getName());
		
		// Getting their names after a set
		u1.setName("Faramir");
		u2.setName("Bono Vox");
		u3.setName("Tom Bombadil");
		assertEquals("Faramir", u1.getName());
		assertEquals("Bono Vox", u2.getName());
		assertEquals("Tom Bombadil", u3.getName());
	}

	@Test
	public void testEqualsObject() throws Exception {
		// Case in which the users differ
		assertEquals(false, u1.equals(u2));
		assertEquals(false, u2.equals(u3));
		assertEquals(false, u3.equals(u1));
		
		// Case in which the users are the same
		u2 = new User("Gimli");
		assertEquals(true, u1.equals(u2));
		u2 = new User("Bilbo");
		assertEquals(true, u3.equals(u2));
	}

}
